package com.ofek.eventos;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.support.v4.content.LocalBroadcastManager;

import com.ofek.eventos.db.ContextDatabase;
import com.ofek.eventos.db.ContextWrapper;

public class ContextListAdapter extends ListAdapter<ContextWrapper>{ 

	private final Activity context;


	public ContextListAdapter(final Activity c, final List<ContextWrapper> list) {
		super(c, R.layout.list_item_event,R.id.title, list);
		context = c;
	}

	/* (non-Javadoc)
	 * @see com.ofek.eventos.ListAdapter#deleteItem(java.lang.Object)
	 */
	@Override
	protected void deleteItem(final ContextWrapper event) {
		final Intent intent = new Intent(ContextDatabase.CONTEXT_REMOVE_REQUEST);
		intent.putExtra(ContextDatabase.CONTEXT_EXTRA, event);
		LocalBroadcastManager.getInstance(context).sendBroadcast(intent);

		super.deleteItem(event);
	}

	/* (non-Javadoc)
	 * @see android.widget.ArrayAdapter#addAll(java.util.Collection)
	 */
	@Override
	public void addAll(final Collection<? extends ContextWrapper> collection) {
		if (Build.VERSION.SDK_INT<Build.VERSION_CODES.HONEYCOMB)  {
			final ArrayList<ContextWrapper> all = ContextDatabase.getInstance().getAll();
			for (final ContextWrapper wrapper : all) add(wrapper);
		} else {
			super.addAll(collection);
		}
	}

}
